package com.service.manage.imp;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mapper.manage.RoleDao;
import com.mapper.manage.UserDao;
import com.mapper.manage.UserRoleDao;
import com.mapper.manage.UserZJDao;
import com.po.User;
import com.po.UserRegister;
import com.po.UserRole;
import com.service.manage.UserRegisterService;
import com.service.manage.UserService;

@Service
public class UserServiceImp implements UserService{
	@Resource(name="userDao")
	private UserDao userDao; 	
	@Resource(name = "userRegisterServiceImp")
	private UserRegisterService userRegisterService;
	@Resource(name = "userRoleDao")
	private UserRoleDao userRoleDao;
	@Resource(name = "userZJDao")
	private UserZJDao userZJDao;
	@Resource(name="roleDao")
	private RoleDao roleDao;
	@Override
	public List<User> getUserList(User user) {
		List<User> list=userDao.getUserList(user);
		return list;
	}
	@Override
	public Integer getUserCount(User user) {
		Integer i=userDao.getUserCount(user);
		return i;
	}
	@Override
	public int deleteUser(String userid) {
		int i=userDao.deleteUser(userid);
		return i;
	}

	@Override
	public int deleteUserList(String[] userid) {
		int i=userDao.deleteUserList(userid);
		return i;
	}
	@Override
	public int addUser(User user) {
		UserRegister userRegister1=new UserRegister();
		userRegister1.setUser_id(user.getUser_id());
		UserRegister userRegister2=userRegisterService.addUserRegisterIsOk(userRegister1);		
		if(userRegister2==null){
			//正常插入一条数据
			UserRegister userRegister=new UserRegister();
			userRegister.setUser_id(user.getUser_id());
			userRegister.setUser_name(user.getUser_name());
			userRegister.setUser_type(user.getUser_style());
			userRegister.setUser_validate(user.getUser_validate());
			
			UserRole userRole=new UserRole(); 
			userRole.setUser_id(user.getUser_id());
			userRole.setRole_id(user.getUser_style());
			
			int result1 = userRegisterService.addUserRegister(userRegister);
			int result2 = userDao.addUser(user);	
			int result3 = userRoleDao.addUserRole(userRole);
			if(result1==1&&result2==1&&result3==1)
				{return 1;}
			else 
				{return 0;}
		}
		//表中已经存在一条该ID的用户，但是是无效用户
		else{
			//如果这条无效数据类型为专家
			if(userRegister2.getUser_type().equals(roleDao.getRoleID("专家"))){
				int result4=userZJDao.deleteUserZJT(user.getUser_id());
				UserRegister userRegister=new UserRegister();
				userRegister.setUser_id(user.getUser_id());
				userRegister.setUser_name(user.getUser_name());
				userRegister.setUser_type(user.getUser_style());
				userRegister.setUser_validate(user.getUser_validate());
				
				UserRole userRole=new UserRole(); 
				userRole.setUser_id(user.getUser_id());
				userRole.setRole_id(user.getUser_style());
				
				int result5 = userRegisterService.updateUserRegister(userRegister);
				int result6 = userDao.addUser(user);	
				int result7 = userRoleDao.updateUserRole(userRole);
				if(result5==1&&result6==1&&result7==1&&result4==1)
					return 1;
				else 
					return 0;
			}
			else{
				UserRegister userRegister=new UserRegister();
				userRegister.setUser_id(user.getUser_id());
				userRegister.setUser_name(user.getUser_name());
				userRegister.setUser_type(user.getUser_style());
				userRegister.setUser_validate(user.getUser_validate());
				
				UserRole userRole=new UserRole(); 
				userRole.setUser_id(user.getUser_id());
				userRole.setRole_id(user.getUser_style());
				
				int result5 = userRegisterService.updateUserRegister(userRegister);
				int result6 = userDao.updateUser(user);	
				int result7 = userRoleDao.updateUserRole(userRole);
				if(result5==1&&result6==1&&result7==1)
					return 1;
				else 
					return 0;
			}
		}
	}
	@Override
	public int updateUser(User user) {
		UserRegister userRegister=new UserRegister();
		userRegister.setUser_id(user.getUser_id());
		userRegister.setUser_name(user.getUser_name());
		userRegister.setUser_type(user.getUser_style());
		userRegister.setUser_validate(user.getUser_validate());
		
		UserRole userRole=new UserRole(); 
		userRole.setUser_id(user.getUser_id());
		userRole.setRole_id(user.getUser_style());
		
		int result1 = userRegisterService.updateUserRegister(userRegister);
		int result2 = userDao.updateUser(user);
		int result3 = userRoleDao.updateUserRole(userRole);
		if(result1==1&&result2==1&&result3==1)
			return 1;
		else 
			return 0;
	}
	@Override
	public int deleteUserT(String user_id) {
		int i=userDao.deleteUserT(user_id);
		return i;
	}
}
